package com.xcc.config;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author frosty
 * @description
 * @date 2019-12-06 06:10:49
 */


public class JsonC {
    public static void main(String[] args) throws Exception{
        File file = new File("/home/swst/data.json");

        List<Coordinate> list=new ArrayList<Coordinate>();

        FileReader reader = new FileReader(file);//定义一个fileReader对象，用来初始化BufferedReader
        BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象，将文件内容读取到缓存
        StringBuilder sb = new StringBuilder();//定义一个字符串缓存，将字符串存放缓存中
        String s = "";
        while ((s =bReader.readLine()) != null) {//逐行读取文件内容，不读取换行符和末尾的空格
            sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
           // System.out.println(s);
        }
        bReader.close();
        String str = sb.toString();



          String replace = str.replace("}", "");

          replace=replace.replace("],","]");
         replace = replace.replace("{", "");
           String[] split = replace.split("]");

        System.out.println(split[2]);
        String[] thanStr=new String[split.length];

                for(int i=0;i<split.length;i++){
           split[i]= split[i].replace(",[","");
            split[i]= split[i].replace("[","");
           // System.out.println(split[i]);
                thanStr[i]=thran(split[i]);

        }
                String sto=Arrays.toString(thanStr);
             saveAsFileWriter(sto);

    }


    public static String thran(String str) {

        System.out.println(str);
        try {
            String[] split = str.split(",");
            Double x = Double.parseDouble(split[0]);
            Double y = Double.parseDouble(split[1]);

            x = x / 20037508.34 * 180;
            y = y / 20037508.34 * 180;
            y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
            String str2 = "["+x+","+y+"]";
            return str2;

        } catch (Exception e) {
            return null;
        }
    }
    private static void saveAsFileWriter(String content) {
        FileWriter fwriter = null;
        try {
            // true表示不覆盖原来的内容，而是加到文件的后面。若要覆盖原来的内容，直接省略这个参数就好
            fwriter = new FileWriter("/home/swst/newdata.json");
            fwriter.write(content);
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
